<?php
/**
 * Xcache缓存驱动器
 * @author Tongle Xu <xutongle@gmail.com> 2012-10-31
 * @copyright Copyright (c) 2003-2103 yuncms.net
 * @license http://leaps.yuncms.net
 * @version $Id: Xcache.php 623 2013-07-29 03:40:03Z 85825770@qq.com $
 */
class Cache_Xcache extends Cache {
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::init()
	 */
	public function init() {
		parent::init ();
		if (! function_exists ( 'xcache_isset' )) {
			throw new Exception ( 'The xcache extension must be loaded !' );
		}
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::setValue()
	 */
	protected function setValue($key, $value, $expire) {
		return xcache_set ( $key, $value, $expire );
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::getValue()
	 */
	protected function getValue($key) {
		return xcache_isset ( $key ) ? xcache_get ( $key ) : false;
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::addValue()
	 */
	protected function addValue($key, $value, $expire) {
		return ! xcache_isset ( $key ) ? $this->setValue ( $key, $value, $expire ) : false;
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::deleteValue()
	 */
	protected function deleteValue($key) {
		return xcache_unset ( $key );
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see Cache::flushValues()
	 */
	protected function flushValues() {
		for($i = 0, $max = xcache_count ( XC_TYPE_VAR ); $i < $max; $i ++) {
			if (xcache_clear_cache ( XC_TYPE_VAR, $i ) === false) return false;
		}
		return true;
	}
}